﻿using System;
using System.Diagnostics;
using System.ServiceProcess;
using OpenDynDnsChanger.Service.Updates.Provider;
using OpenDynDnsChanger.Service.Updates;

namespace OpenDynDnsChanger.Service
{
    public partial class ODynDnsChanger : ServiceBase
    {
        public ODynDnsChanger()
        {
            InitializeComponent();
        }

        Updates.IDnsUpdater updateProvider;
        System.Timers.Timer timer;
        OpenDynDnsChanger.Cryption.CryptionHelper cryptionHelper;

        protected override void OnStart(string[] args)
        {
            try
            {
                ConfigHelper.Init();

                cryptionHelper = new Cryption.CryptionHelper();

                LoadProvider();

                //Start the timer
                timer = new System.Timers.Timer();
                timer.Interval = ConfigHelper.UpdateTime;
                timer.Elapsed += timer_Elapsed;
                timer.Start();

                if (timer.Enabled)
                    System.Diagnostics.EventLog.WriteEntry(Consts.ServiceName, "Timer Startet! " + timer.Interval.ToString() + "ms");

                System.Diagnostics.EventLog.WriteEntry(Consts.ServiceName, "Service started");

            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry(Consts.ServiceName, ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error);
            }
        }

        private void LoadProvider()
        {
            string providerName = ConfigHelper.Provider;

            switch (providerName.ToLower())
            {
                case "noip":
                    updateProvider = new SP_NoIP();
                    return;
                default:
                    System.Diagnostics.EventLog.WriteEntry(Consts.ServiceName, "Cannot load Service Provider");
                    this.Stop();
                    return;
            }

        }

        /// <summary>
        /// Updates the DynDNS Entry
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (updateProvider != null)
                {
                    System.Diagnostics.EventLog.WriteEntry(Consts.ServiceName, "Call Updater");
                    updateProvider.Callback = new Updates.DnsUpdateCallback(UpdateComplete);
                    updateProvider.Update(Helper.IPHelper.GetIP(), ConfigHelper.Username, cryptionHelper.Password);
                }
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error);
            }
        }

        private void UpdateComplete(UpdateCallbackResult result)
        {
            System.Diagnostics.EventLog.WriteEntry(Consts.ServiceName, "Got Result: " + result.ResultFlag.ToString() + " | Raw: " + result.RawResult);
        }


        protected override void OnStop()
        {
            timer.Stop();
            System.Diagnostics.EventLog.WriteEntry(Consts.ServiceName, "Dienst beendet");
        }
        


    }
}
